package Boundary;
import Entity.RolesUndertaken;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.List;

import static Control.Control.*;

/**
 *Title       :RolesManagement.java
 *Description: This class initialize GUI
 * @author Group77
 * @version 4.0
 * @description display an operational page for roles
 */
public class RolesManagement extends Overall{
    public RolesManagement(int number){
        super("Roles Management");

        //Return
        panel1.setLayout(new BorderLayout());
        panel1.setBorder(BorderFactory.createEmptyBorder());
        JButton button0 = new JButton("   Go back   ");
        button0.setBorder(BorderFactory.createRaisedBevelBorder());
        button0.setBackground(Color.cyan);
        button0.setFont(new Font("STSong", Font.BOLD, 30));
        JLabel label1 = new JLabel("Roles Undertaken  ", JLabel.CENTER);
        label1.setFont(new Font("TimesRoman", Font.ITALIC, 30));
        JPanel empty1 = new JPanel();
        empty1.setPreferredSize(new Dimension(100, 10));
        panel1.add(button0, BorderLayout.WEST);
        panel1.add(empty1, BorderLayout.NORTH);
        panel1.add(label1, BorderLayout.EAST);
        Add_Component(jfr, gbl, panel1, gbc, 0, 0, 1, 1, 1, 0);


        button0.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        jfr.setVisible(false);
                        new Start(number);
                        jfr.dispose();
                    }
                }
        );



        panel3.setLayout(new BorderLayout());
        panel3.setBorder(BorderFactory.createEmptyBorder());
        Object[] columnNames = { "Role Name", "Semester", "Credit","Volunteer duration"};
        List<RolesUndertaken> roles=json3.readArray(RolesUndertaken.class);


        List list = new ArrayList();
        int i = 0;
        while(i < roles.size()){
            if(number == roles.get(i).getStudentsNumber()){
                String[] str = {roles.get(i).getRoleName(),roles.get(i).getSemester(),roles.get(i).getCredits(),roles.get(i).getVoluntary_duration()};
                list.add(str);
            }
            i++;
        }
        Object[] s = (Object[])list.toArray();
        String[][] rowData = new String[s.length][];
        for(int j=0;j<s.length;j++){
            rowData[j] = (String[])s[j];
        }
        int totalCredits = 0;
        int totalDuration = 0;
        i=0;

        while (i < roles.size()){
            if(number == roles.get(i).getStudentsNumber()){
                totalCredits = totalCredits + (Integer.parseInt(roles.get(i).getCredits()));
            }
            i++;
        }
        i=0;
        while(i<roles.size()){
            if(number == roles.get(i).getStudentsNumber()){
                System.out.println(roles.get(i).getVoluntary_duration());
                totalDuration = totalDuration + (Integer.parseInt(roles.get(i).getVoluntary_duration()));
            }
            i++;
        }

        String a = "Your total credit is " + totalCredits;
        String b = "Your total volunteer duration is " + totalDuration;



//        Object[][] rowData = {
//                {modules.get(0).getStudentsNumber(),modules.get(0).getRoleName(),modules.get(0).getSemester(),modules.get(0).getCredits()},
//                //{modules.get(1).getModuleName(),modules.get(1).getModuleNumber(),modules.get(1).getSemester(),modules.get(1).getCredits(),modules.get(1).getGrades()},
//                {"2020213456", "class rep", "2", "1.5"},
//                {"2020213456", "software engineering module rep", "1", "1"},
//                {"2020213548", "Winter Olympics volunteer", "1", "5"}
//        };

        JTable table1 = new JTable (rowData, columnNames);
        table1.setForeground(Color.BLACK);
        table1.setFont(new Font(null, Font.PLAIN, 14));
        table1.setSelectionForeground(Color.DARK_GRAY);
        table1.setSelectionBackground(Color.cyan);
        table1.setGridColor(Color.GRAY);
        table1.setBackground(Color.white);
        table1.getTableHeader().setFont(new Font(null, Font.BOLD, 14));
        table1.getTableHeader().setForeground(Color.BLACK);
        table1.getTableHeader().setBackground(new Color(113,191,234));
        table1.getTableHeader().setResizingAllowed(false);
        table1.getTableHeader().setReorderingAllowed(false);
        table1.setRowHeight(30);
        table1.getColumnModel().getColumn(0).setPreferredWidth(40);
        JScrollPane scrollPane = new JScrollPane(table1);
        table1.setPreferredScrollableViewportSize(new Dimension(400, 420));
        panel3.add(scrollPane);
        Add_Component(jfr,gbl,panel3,gbc,0,2,1,1,1,0);

        panel4.setLayout(new BorderLayout());
        panel4.setBorder(BorderFactory.createEmptyBorder());
        JLabel label4 = new JLabel("<html><body>Roles Analysis" + "<br>" + a + "<br>" + b + "</body></html>", JLabel.CENTER);
        label4.setFont(new Font("TimesRoman", Font.ITALIC, 25));
        panel4.add(label4, BorderLayout.CENTER);
        Add_Component(jfr, gbl, panel4, gbc, 1, 2, 1, 1, 1, 0);

        panel5.setLayout(new BorderLayout());
        panel5.setBorder(BorderFactory.createEmptyBorder());
        JButton button1 = new JButton(" Update  ");
        JButton button2 = new JButton(" Delete  ");
        button2.setBorder(BorderFactory.createRaisedBevelBorder());
        button2.setBackground(Color.cyan);
        button2.setFont(new Font("STSong", Font.BOLD, 25));
        button1.setBorder(BorderFactory.createRaisedBevelBorder());
        button1.setBackground(Color.cyan);
        button1.setFont(new Font("STSong", Font.BOLD, 25));
        JPanel empty3 = new JPanel();
        empty1.setPreferredSize(new Dimension(100, 10));
        panel5.add(button1, BorderLayout.WEST);
        panel5.add(button2, BorderLayout.EAST);
        panel5.add(empty3, BorderLayout.NORTH);
        Add_Component(jfr, gbl, panel5, gbc, 0, 6, 1, 1, 1, 0);


        button1.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        jfr.setVisible(false);
                        new RoleAdd(number);
                        jfr.dispose();
                    }
                }
        );

        button2.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        jfr.setVisible(false);
                        new RoleDelete(number);
                        jfr.dispose();
                    }
                }
        );

    }
}
